<template>
  <div class="login">
    <el-form ref="form" class="form" :model="form" :rules="rules">
      <img class="img" src="@/assets/common/login-logo.png" alt="" />
      <el-form-item prop="mobile">
        <el-input
          placeholder="请输入账号"
          prefix-icon="el-icon-user-solid"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          placeholder="请输入密码"
          type="password"
          prefix-icon="el-icon-unlock"
          v-model="form.password"
          @keyup.enter.native="submit"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" type="primary" @click="submit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { checkPhone } from '@/utils/validate.js'
// import { sysLogin } from '@/api/user'
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      form: {
        mobile: '13600000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              checkPhone(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            }
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '请输入6-16位密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['getUserToken']),
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          // this.$message.success('验证成功')
          // const res = await sysLogin(this.form)
          // this.$store.dispatch('user/getUserToken', this.form)
          // this.getUserToken(this.form).then(() => {
          // this.$router.push('/')
          // })
          // try {
            await this.getUserToken(this.form)
            this.$router.push('/')
          // } catch (error) {
          //   window.console.log(error)
          // }
        } else {
          this.$message.error('验证失败')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  min-height: 100%;
  background: url(~@/assets/common/login.jpg);
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  .form {
    width: 500px;
    text-align: center;
    .img {
      margin-bottom: 15px;
    }
    .btn {
      width: 100%;
      background-color: #407ffe;
      border-color: #407ffe;
      color: #fff;
    }
    ::v-deep .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
